#include "CRL_VertexBufferDescriptor.h"
#include "CBL_Debug.h"

CB_SUBNAMESPACE_START(RenderLib)

#define ENUM_ITEM(_Name, _Size, _Normlized, _Elements, _OpenGLFormat, _D3D9Format, _D3D10Format) _Size,
const uint32 CVertexBufferDescriptor::s_VertexFormatSizeByte[CVertexBufferDescriptor::NumVertexFormats] = 
{
	0,
	#include "CRL_VertexFormatsEnum.h"
};
#undef ENUM_ITEM

// -------------------------------------------------------------------------------------------

#define ENUM_ITEM(_Name, _Size, _Normlized, _Elements, _OpenGLFormat, _D3D9Format, _D3D10Format) #_Name,
const char* CVertexBufferDescriptor::s_VertexFormatNames[CVertexBufferDescriptor::NumVertexFormats] =
{
	"Unused",
	#include "CRL_VertexFormatsEnum.h"
};
#undef ENUM_ITEM

// -------------------------------------------------------------------------------------------

#define ENUM_ITEM(_Name) #_Name,
const char* CVertexBufferDescriptor::s_VertexSlotNames[CVertexBufferDescriptor::NumVertexSlots] =
{
	"Invalid",
	#include "CRL_SlotNameEnum.h"
};
#undef ENUM_ITEM

// -------------------------------------------------------------------------------------------

#define ENUM_ITEM(_Name, _Size, _Normlized, _Elements, _OpenGLFormat, _D3D9Format, _D3D10Format) _Normlized,
const bool CVertexBufferDescriptor::s_VertexFormatNormalized[NumVertexFormats] =
{
    false,
    #include "CRL_VertexFormatsEnum.h"
};
#undef ENUM_ITEM

// -------------------------------------------------------------------------------------------

#define ENUM_ITEM(_Name, _Size, _Normlized, _Elements, _OpenGLFormat, _D3D9Format, _D3D10Format) _Elements,
const uint32 CVertexBufferDescriptor::s_VertexFormatElements[NumVertexFormats] =
{
    0,
    #include "CRL_VertexFormatsEnum.h"
};

// -------------------------------------------------------------------------------------------

CVertexBufferDescriptor::CVertexBufferDescriptor()
: m_Stride(0)
, m_SlotCount(0)
, m_StorageType(ManagedStorage)
, m_VertexCount(0)
{
	for (uint32 i=0; i<NumVertexSlots; ++i)
	{
		m_SlotFormat[i] = Unused;
		m_SlotType[i]   = Invalid;
	}
}

// -------------------------------------------------------------------------------------------

uint32 CVertexBufferDescriptor::AddSlot(CVertexBufferDescriptor::EVertexSlot _SlotType, CVertexBufferDescriptor::EVertexFormat _SlotFormat)
{
	if (Debug::Verify(m_SlotCount < NumVertexSlots))
	{
		m_SlotType[m_SlotCount]   = _SlotType;
		m_SlotFormat[m_SlotCount] = _SlotFormat;
		
		m_Stride += s_VertexFormatSizeByte[_SlotFormat];
		return m_SlotCount++;
	}
	
	return m_SlotCount;
}

// -------------------------------------------------------------------------------------------

void CVertexBufferDescriptor::CalculateStride()
{
	m_Stride = 0;
	
	for (uint32 i=0; i<m_SlotCount; ++i)
		m_Stride += s_VertexFormatSizeByte[m_SlotFormat[i]];
}


CB_SUBNAMESPACE_END